#!/usr/bin/env python
# coding: utf-8
# sys._getframe().f_back.f_code.co_name

from engine.common import *
from shutil import copyfile

# debugInfo("checknow ",sys._getframe().f_code.co_name)
class screenClass:
    """screen"""
    currentScreen = ""
    backN = 30 #备份最近的30帧画面
    backNScreen=[]
    width  = 0
    height = 0
    type =""
    sno = ""

    def shotNow(self, screenName,sname):
    #拍照函数
        #备份最近的N个记录画面

        # for i in range(self.backN-3,-1,-1):
        #     if os.path.exists(self.backNScreen[i]):
        #         try:
        #             copyfile(self.backNScreen[i], self.backNScreen[i+1])
        #         except IOError as e:
        #             print("Unable to copy file. %s" % e)
        #             exit(1)
        #         except:
        #             print("Unexpected error:", sys.exc_info())
        #             exit(1)
        # if os.path.exists(self.currentScreen):
        #     try:
        #         copyfile(self.currentScreen, self.backNScreen[0])
        #     except IOError as e:
        #         print("Unable to copy file. %s" % e)
        #         exit(1)
        #     except:
        #         print("Unexpected error:", sys.exc_info())
        #         exit(1)

        # if os.path.exists(self.backNScreen[0]):
        #     copyfile(self.backNScreen[0],  os.getcwd()+"/cut/"+self.type+"/"+self.sno+"/"+sname+'.png')

        screen = shotScreen('current')

        copyfile(self.currentScreen, os.getcwd() + "/cut/" + self.type + "/" + self.sno + "/" + sname + '.png')
        return self.currentScreen

    def __init__(self):
        self.DEBUG = True
        debugInfo("__init__  ", sys._getframe().f_back.f_code.co_name)
        path = self.shotNow()
        debugInfo(str(path))
        img = Image.open(path)

        debugInfo(str(img.format))
        debugInfo(str(img.size))
        self.width,self.height = img.size
        self.displayInfo()

    def __init__(self,type,sno):
        self.DEBUG = True
        debugInfo("__init__  ")
        self.type = type
        self.sno = sno
        self.currentScreen = os.getcwd()+"/cut/"+self.type+"/"+self.sno+"/"+"current.png"
        for i in range(1,self.backN):
            self.backNScreen.append(os.getcwd()+"/cut/"+self.type+"/"+self.sno+"/"+"back"+str(i)+"Screen.png")
            #print(self.backNScreen[i-1])


    def displayInfo(self):
        debugInfo("---------------display info-------------------------------")
        debugInfo(self.width)
        debugInfo(self.height)
        debugInfo(self.currentScreen)
        debugInfo(self.back1Screen)
        debugInfo("---------------display info-------------------------------")

    def getCurrentScreen(self, sname = "test",screenName="current"):
        currentScreen = self.shotNow(screenName,sname)
        return currentScreen

    def backToScreen(self,backNumber):
        if backNumber == 1:
            debugInfo("")
        elif backNumber ==2:
            debugInfo("")
